package com.fjb.controller.upload;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.aliyun.oss.OSSClient;
import com.fjb.common.aliyun.AliYunOssConfig;
import com.fjb.common.entity.HttpCode;
import com.fjb.common.entity.JsonResult;
import com.fjb.common.enums.DataSource;
import com.fjb.common.enums.file.FileStatus;
import com.fjb.common.enums.user.FileSource;
import com.fjb.common.global.GlobalConfig;
import com.fjb.common.utils.FileSizeUtil;
import com.fjb.common.utils.SerialNumberUtils;
import com.fjb.common.utils.UploadUtils;
import com.fjb.controller.base.BaseController;
import com.fjb.dto.LoginUserInfo;
import com.fjb.pojo.user.SysUserFile;
import com.fjb.service.user.SysUserFileService;

/**
 * @Description:上传
 * @author hemiao
 * @time:2020年1月4日 下午8:58:44
 */
@Controller
@RequestMapping("/upload")
public class UploadController extends BaseController{
	
	@Autowired
	private SysUserFileService sysUserFileService;
	
	/**
	 * @Description:webuploader 测试
	 * @param file
	 * @param request
	 * @return
	 * JsonResult<Map<String,String>>
	 * @exception:
	 * @author: hemiao
	 * @time:2019年5月28日 下午9:30:09
	 */
    @RequestMapping(value = "/webuploader")
    @ResponseBody
    public JsonResult<Map<String,String>> webuploader(@RequestParam("file") MultipartFile file, HttpServletRequest request){
        Map <String, String> resuletMap = new HashMap <String, String>();
        //图片文件类型
        String contentType = file.getContentType();   
        //图片名字
        String fileName = file.getOriginalFilename();  
        System.out.printf(contentType);
        System.out.printf(fileName);
        resuletMap.put("newFileName","");
        return new JsonResult<Map<String,String>>(resuletMap, HttpCode.SUCCESS);
    }
    
    /**
     * @Description:意见反馈文件上传
     * @param file
     * @param request
     * @return
     * JsonResult<Map<String,Object>>
     * @exception:
     * @author: hemiao
     * @time:2020年2月13日 下午9:01:09
     */
    @RequestMapping(value = "/feedbackFile")
    @ResponseBody
    public JsonResult<Map<String,Object>> feedbackFile(@RequestParam("file") MultipartFile file, 
    		HttpServletRequest request){
        HttpCode httpCode = HttpCode.PARAM_VERIFICATION;
    	Map <String, Object> resuletMap = new HashMap <String, Object>();
        // 图片名字
        String fileName = file.getOriginalFilename(); 
        // 图片文件类型	
        String contentType = file.getContentType();
        // 获得当前登录用户信息	
        LoginUserInfo userInfo = getBaseWxTokenId(request);
        Integer createUserId = userInfo.getUserId();
        Integer mainUserId = userInfo.getMainUserId();
		// 获取文件大小
		long fileSize = file.getSize();
		if (fileSize == 0) {
			httpCode.setMsg("文件不能为空");
			return new JsonResult<Map<String, Object>>(null, httpCode);
		}
		double newFileSize = FileSizeUtil.formetFileSize(fileSize, FileSizeUtil.SIZETYPE_MB);
		if (newFileSize > 5) {	
			httpCode.setMsg("上传文件的大小不能超过5MB");
			return new JsonResult<Map<String, Object>>(null, httpCode);
		}
		// 新的文件名称		
		String newFileName = GlobalConfig.FILE_FEEDBACK + UploadUtils.getNewFileName(fileName);
		OSSClient ossClient = AliYunOssConfig.getOSSClient();
		// 获取文件流
		try {
			// 获取文件流		
			InputStream is = file.getInputStream();
			ossClient.putObject(GlobalConfig.OSS_BUCKET_NAME, newFileName, is);
		} catch (Exception e) {
			e.printStackTrace();
			return new JsonResult<Map<String, Object>>(null, HttpCode.EXCEPTIONS);
		}
		// 创建编号
        resuletMap.put("newFileName",newFileName);
        String fileServiceUrl = GlobalConfig.FILE_SERVICE_LOCATION;
        resuletMap.put("fileUrl",  fileServiceUrl+ newFileName);

        LocalDateTime localDateTime = LocalDateTime.now();
        // 保存用户文件日志
        String number = SerialNumberUtils.createNumber(SerialNumberUtils.USER_FILE, mainUserId);
        SysUserFile suf = new SysUserFile();
        suf.setFileNumber(number);
        suf.setMainUserId(mainUserId);
        suf.setFileSize(newFileSize);
        suf.setContentType(contentType);
        suf.setFileUrl(newFileName);
        suf.setFileServiceUrl(fileServiceUrl);
        suf.setUserId(mainUserId);
        suf.setCreateUserId(createUserId);
        suf.setCreateTime(localDateTime);
        suf.setFileStatus(FileStatus.STATUS_1.getCode());
        suf.setFileSource(FileSource.SOURCE_5.getCode());
        suf.setDataSource(DataSource.WX_APPLET.getCode());
        sysUserFileService.save(suf);			
        return new JsonResult<Map<String,Object>>(resuletMap, HttpCode.SUCCESS);
    }
    
    
    /**
     * @Description:收购 文件上传
     * @param file
     * @param request
     * @return
     * JsonResult<Map<String,Object>>
     * @exception:
     * @author: hemiao
     * @time:2020年2月16日 下午4:14:32
     */
    @RequestMapping(value = "/recycleFiles")
    @ResponseBody
    public JsonResult<Map<String,Object>> recycleFiles(@RequestParam("file") MultipartFile file, 
    		HttpServletRequest request){
        HttpCode httpCode = HttpCode.PARAM_VERIFICATION;
    	Map <String, Object> resuletMap = new HashMap <String, Object>();
        // 图片名字
        String fileName = file.getOriginalFilename(); 
        // 图片文件类型	
        String contentType = file.getContentType();
        // 获得当前登录用户信息	
        LoginUserInfo userInfo = getBaseWxTokenId(request);
        Integer createUserId = userInfo.getUserId();
        Integer mainUserId = userInfo.getMainUserId();
		// 获取文件大小
		long fileSize = file.getSize();
		if (fileSize == 0) {
			httpCode.setMsg("文件不能为空");
			return new JsonResult<Map<String, Object>>(null, httpCode);
		}
		double newFileSize = FileSizeUtil.formetFileSize(fileSize, FileSizeUtil.SIZETYPE_MB);
		if (newFileSize > 5) {	
			httpCode.setMsg("上传文件的大小不能超过5MB");
			return new JsonResult<Map<String, Object>>(null, httpCode);
		}
		// 新的文件名称			
		String newFileName = GlobalConfig.FILE_RECYCLE + UploadUtils.getNewFileName(fileName);
		OSSClient ossClient = AliYunOssConfig.getOSSClient();
		// 获取文件流
		try {
			// 获取文件流	
			InputStream is = file.getInputStream();
			//ossClient.putObject(GlobalConfig.OSS_BUCKET_NAME, newFileName, is);
		} catch (Exception e) {
			e.printStackTrace();
			return new JsonResult<Map<String, Object>>(null, HttpCode.EXCEPTIONS);
		}
		// 创建编号
        resuletMap.put("newFileName",newFileName);
        String fileServiceUrl = GlobalConfig.FILE_SERVICE_LOCATION;
        resuletMap.put("fileUrl",  fileServiceUrl+ newFileName);

        LocalDateTime localDateTime = LocalDateTime.now();
        // 保存用户文件日志
        String number = SerialNumberUtils.createNumber(SerialNumberUtils.USER_FILE, mainUserId);
        SysUserFile suf = new SysUserFile();
        suf.setFileNumber(number);
        suf.setMainUserId(mainUserId);
        suf.setFileSize(newFileSize);
        suf.setContentType(contentType);
        suf.setFileUrl(newFileName);
        suf.setFileServiceUrl(fileServiceUrl);
        suf.setUserId(mainUserId);
        suf.setCreateUserId(createUserId);
        suf.setCreateTime(localDateTime);
        suf.setFileStatus(FileStatus.STATUS_1.getCode());
        suf.setFileSource(FileSource.SOURCE_6.getCode());
        suf.setDataSource(DataSource.WX_APPLET.getCode());
        sysUserFileService.save(suf);			
        return new JsonResult<Map<String,Object>>(resuletMap, HttpCode.SUCCESS);
    }
    
    /**
     * @Description:产品文件上传
     * @param file
     * @param request
     * @return
     * JsonResult<Map<String,Object>>
     * @exception:
     * @author: hemiao
     * @time:2020年2月16日 下午4:15:54
     */
    @RequestMapping(value = "/productFiles")
    @ResponseBody
    public JsonResult<Map<String,Object>> productFiles(@RequestParam("file") MultipartFile file, 
    		HttpServletRequest request){
        HttpCode httpCode = HttpCode.PARAM_VERIFICATION;
    	Map <String, Object> resuletMap = new HashMap <String, Object>();
        // 图片名字
        String fileName = file.getOriginalFilename(); 
        // 图片文件类型	
        String contentType = file.getContentType();
        // 获得当前登录用户信息	
        LoginUserInfo userInfo = getBaseWxTokenId(request);
        Integer createUserId = userInfo.getUserId();
        Integer mainUserId = userInfo.getMainUserId();
		// 获取文件大小
		long fileSize = file.getSize();
		if (fileSize == 0) {
			httpCode.setMsg("文件不能为空");
			return new JsonResult<Map<String, Object>>(null, httpCode);
		}
		double newFileSize = FileSizeUtil.formetFileSize(fileSize, FileSizeUtil.SIZETYPE_MB);
		if (newFileSize > 5) {	
			httpCode.setMsg("上传文件的大小不能超过5MB");
			return new JsonResult<Map<String, Object>>(null, httpCode);
		}
		// 新的文件名称			
		String newFileName = GlobalConfig.FILE_PRODUCT + UploadUtils.getNewFileName(fileName);
		OSSClient ossClient = AliYunOssConfig.getOSSClient();
		// 获取文件流
		try {	
			// 获取文件流	
			InputStream is = file.getInputStream();
			ossClient.putObject(GlobalConfig.OSS_BUCKET_NAME, newFileName, is);
		} catch (Exception e) {
			e.printStackTrace();
			return new JsonResult<Map<String, Object>>(null, HttpCode.EXCEPTIONS);
		}
		// 创建编号
        resuletMap.put("newFileName",newFileName);
        String fileServiceUrl = GlobalConfig.FILE_SERVICE_LOCATION;
        resuletMap.put("fileUrl",  fileServiceUrl+ newFileName);

        LocalDateTime localDateTime = LocalDateTime.now();
        // 保存用户文件日志	
        String number = SerialNumberUtils.createNumber(SerialNumberUtils.USER_FILE, mainUserId);
        SysUserFile suf = new SysUserFile();
        suf.setFileNumber(number);
        suf.setMainUserId(mainUserId);
        suf.setFileSize(newFileSize);
        suf.setContentType(contentType);
        suf.setFileUrl(newFileName);
        suf.setFileServiceUrl(fileServiceUrl);
        suf.setUserId(mainUserId);
        suf.setCreateUserId(createUserId);
        suf.setCreateTime(localDateTime);
        suf.setFileStatus(FileStatus.STATUS_1.getCode());
        suf.setFileSource(FileSource.SOURCE_2.getCode());
        suf.setDataSource(DataSource.WX_APPLET.getCode());
        sysUserFileService.save(suf);			
        return new JsonResult<Map<String,Object>>(resuletMap, HttpCode.SUCCESS);
    }
}
